package com.kongfuzi.student.ui.global;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kongfuzi.student.R;
import com.kongfuzi.student.bean.MessageEvent;
import com.kongfuzi.student.bean.User;
import com.kongfuzi.student.support.utils.ToastUtil;
import com.kongfuzi.student.ui.base.BaseActivity;
import com.kongfuzi.student.ui.usercenter.ResetPwdActivity;
import com.kongfuzi.student.app.GasApplication;
import com.kongfuzi.student.support.utils.RequestUtil;
import com.kongfuzi.student.support.utils.UrlConstants;
import com.kongfuzi.student.support.volley.Response;
import com.kongfuzi.student.ui.usercenter.LoginActivity;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

/**
 * Created by cfan_yang on 2015/12/24.
 */
public class MenuFragment extends Fragment implements View.OnClickListener{

    @Bind(R.id.tv_number)
    TextView mTv_number;
    @Bind(R.id.tv_welcome)
    TextView mTv_welcome;
    @Bind(R.id.btn_login)
    Button mBtn;
    @Bind(R.id.ll_change_pwd)
    LinearLayout mLl_change_pwd;
    @Bind(R.id.ll_about_us)
    LinearLayout mLl_about;
    private final static String mTag = "MenuFragment";


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        EventBus.getDefault().register(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        View root = View.inflate(getContext(), R.layout.fragment_left_menu,null);
        ButterKnife.bind(this, root);
        resetLoginState();
        initEvents();
        return root;
    }

    private void initEvents() {
        mBtn.setOnClickListener(this);
        mLl_change_pwd.setOnClickListener(this);
        mLl_about.setOnClickListener(this);
    }

    private void resetLoginState() {
        if(GasApplication.isLogin()) {
            mTv_number.setText(GasApplication.getMobileNubmer());
            mTv_welcome.setVisibility(View.VISIBLE);
            mBtn.setText("退出登录");
        } else {
            mTv_number.setText("未登录");
            mTv_welcome.setVisibility(View.INVISIBLE);
            mBtn.setText("登录");
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                if(GasApplication.isLogin()) {
                    FragmentActivity activity = getActivity();
                    if(activity instanceof BaseActivity) {
                        ((BaseActivity)activity).showProgressDialogNotCancel("正在退出");
                    }
                    String requestUrl = UrlConstants.LOG_OUT + "&id=" + GasApplication.getUserId();
                    RequestUtil.requestGet(requestUrl, new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            EventBus.getDefault().post(new MessageEvent(MessageEvent.MessageType.DISMISS_PROGRESS_DIALOG));
                            if(response!=null && response.optBoolean("success")) {
                                User user = new User("","","","");
                                GasApplication.saveUser(user);
                                resetLoginState();
                                ToastUtil.showToast(getContext(), "退出登录成功");
                            }
                        }
                    }, null, mTag);
                } else {
                    startActivity(LoginActivity.newIntent());
                }
                break;
            case R.id.ll_change_pwd:
                if(GasApplication.isLogin()) {
                    startActivity(ResetPwdActivity.newIntent());
                } else {
                    startActivity(LoginActivity.newIntent());
                }
                break;
            case R.id.ll_about_us:
                startActivity(AboutUsActivity.newIntent());
                break;
        }
    }

    public void onEvent(MessageEvent event){
        if(MessageEvent.MessageType.REFRESH_MENU_CONTENT == event.messageType) {
            resetLoginState();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
